package com.example.petstore.controller;

import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.CartItem;
import com.example.petstore.domain.Item;
import com.example.petstore.domain.User;
import com.example.petstore.service.CartService;
import com.example.petstore.service.ItemService;
import com.example.petstore.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.HashMap;
import java.util.List;

@Controller
@Slf4j
@SessionAttributes({"loginUser"})
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private ItemService itemService;

    /**
     * 用户加入商品到购物车
     * @param itemId
     * @param model
     * @return
     */
    @GetMapping("/user/addToCart")
    @ResponseBody
    public CommonResponse userAddToCart(String itemId, ModelMap model){
        User user = (User) model.getAttribute("loginUser");
        if(user == null){
//            System.out.println(user);
            return CommonResponse.creatForError("用户尚未登录，请先登录");
        }
        return cartService.userAddItemToCart(user.getUsername(), itemId, 1);
    }

    /**
     * 更新购物车中某个单独的商品
     * @param itemId
     * @param number
     * @param model
     * @return
     */
    @PostMapping("/user/cart/update")
    @ResponseBody
    public CommonResponse userUpdateCart(String itemId, int number, ModelMap model){
        User user = (User) model.getAttribute("loginUser");
        return cartService.userUpdateCart(user.getUsername(), itemId, number);
    }

    /**
     * 删除购物车中商品
     * @param itemId
     * @param model
     * @return
     */
    @PostMapping("/user/cart/delete")
    @ResponseBody
    public CommonResponse userDeleteCartItem(String itemId, ModelMap model){
        User user = (User) model.getAttribute("loginUser");
        return cartService.userDeleteCartItem(user.getUsername(), itemId);
    }

    /**
     * 进入购物车
     * @param model
     * @return
     */
    @GetMapping("/user/cart")
    public String userCart(ModelMap model){
        User user = (User) model.getAttribute("loginUser");
        CommonResponse commonResponse = cartService.getUerCartItemList(user.getUsername());
        if(commonResponse.isSuccess()){
            HashMap<CartItem, Item> cartItemItemHashMap = new HashMap<>();
            for(CartItem cartItem : (List<CartItem>)commonResponse.getData()) {
                Item item = (Item) itemService.getItemByItemid(cartItem.getItemid()).getData();
                cartItemItemHashMap.put(cartItem, item);
            }
            if(cartItemItemHashMap.isEmpty())
                model.addAttribute("cartItemListMap", null);
            else
                model.addAttribute("cartItemListMap", cartItemItemHashMap);
//            到购物车页面
            return "cartOrder/cart";
        }
        model.addAttribute("errorMsg", commonResponse.getMessage());
//        todo:原页面
        return "catalog/main";
    }
}
